package com.ncbee.base.config.shiro;

import com.ncbee.base.config.shiro.aop.ExtShiroAdvisor;
import com.ncbee.base.config.shiro.filter.CustomUserAccessFilter;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * @Author fengzirong
 * @DESC
 * @Date 2018/12/9 14:42
 **/
@Configuration
public class CustomShiroConfig {
    private static final Logger logger = LoggerFactory.getLogger(CustomShiroConfig.class);


    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //这里添加自定义filter
        shiroFilterFactoryBean.getFilters().put("userAccess",customUserAccessFilter());
        Map<String,String> map = new LinkedHashMap<>();
        //设置静态资源都可以访问
        map.put("/css/**","anon");
        map.put("/images/**","anon");
        map.put("/js/**","anon");
        map.put("/login","anon");
        map.put("/loginPage","anon");
        map.put("/kaptcha","anon");
        map.put("/logout","logout");
        //设置记住我，之后可以访问的连接
        map.put("/common/index","user");
        //对所有用户认证
        map.put("/**","userAccess,authc");
        //登录
        shiroFilterFactoryBean.setLoginUrl("/loginPage");
        //首页
        shiroFilterFactoryBean.setSuccessUrl("/common/index");
        //错误页面，认证不通过跳转
        shiroFilterFactoryBean.setUnauthorizedUrl("/error/unAccess");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }
    @Bean
    public CustomUserAccessFilter customUserAccessFilter(){
        CustomUserAccessFilter customUserAccessFilter=new CustomUserAccessFilter();
        return customUserAccessFilter;
    }
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new ExtShiroAdvisor();
        advisor.setSecurityManager(securityManager());
        return advisor;
    }
    /**
     * 构造 SecurityManager
     * @return*/
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

        securityManager.setRealm(customShiroRealm());
        //shiro session管理
        securityManager.setSessionManager(sessionManager());
        //记住我功能
        securityManager.setRememberMeManager(cookieRememberMeManager());
        return securityManager;
    }

    /**
     * shiro Session管理
     *  默认失效 时间 为 30分钟
     * @return
     */
    public SessionManager sessionManager(){
        DefaultWebSessionManager sessionManager=new DefaultWebSessionManager();
        return sessionManager;
    }
    /*
     *
     * 自定义权限规则
     * @return
     * */
    @Bean
    public CustomShiroRealm customShiroRealm(){
        CustomShiroRealm customShiroRealm=new CustomShiroRealm();
        return customShiroRealm;
    }
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        LifecycleBeanPostProcessor lifecycleBeanPostProcessor=new LifecycleBeanPostProcessor();
        return lifecycleBeanPostProcessor;
    }

    @Bean
    public CookieRememberMeManager cookieRememberMeManager(){
        CookieRememberMeManager cookieRememberMeManager=new CookieRememberMeManager();
        //<!-- 记住我cookie生效时间30天 ,单位秒-->
        cookieRememberMeManager.getCookie().setMaxAge(2592000);
        cookieRememberMeManager.setCipherKey(Base64.decode("4AvVhmFLUs0KTA3Kprsdag=="));
        return cookieRememberMeManager;
    }

}
